package com.hsurosy.hsuchain.nft.job.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * XxlJob 配置类
 *
 * @Author Hsu琛君珩
 * @Date 2024-10-05 4:32
 * @Description
 * @Version: v1.0.0
 */
@Configuration
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobConfiguration {

    /**
     * 日志记录器，用于输出日志信息
     */
    private static final Logger logger = LoggerFactory.getLogger(XxlJobConfiguration.class);

    /**
     * 自动注入 XxlJobProperties 属性配置类
     */
    @Autowired
    private XxlJobProperties properties;

    /**
     * 创建并配置 XXL-JOB 执行器
     * 该方法用于初始化 XXL-JOB 的 Spring 执行器，并设置相关参数如管理员地址、应用名称、IP、端口、访问令牌等。
     *
     * @return 返回配置好的 XXL-JOB 执行器实例
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = XxlJobProperties.PREFIX, value = "enabled", havingValue = "true")
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init."); // 输出初始化日志信息

        // 创建并配置 XXL-JOB Spring 执行器
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(properties.getAdminAddresses()); // 设置管理员地址
        xxlJobSpringExecutor.setAppname(properties.getAppName()); // 设置应用名称
        xxlJobSpringExecutor.setIp(properties.getIp()); // 设置 IP 地址
        xxlJobSpringExecutor.setPort(properties.getPort()); // 设置端口号
        xxlJobSpringExecutor.setAccessToken(properties.getAccessToken()); // 设置访问令牌
        xxlJobSpringExecutor.setLogPath(properties.getLogPath()); // 设置日志路径
        xxlJobSpringExecutor.setLogRetentionDays(properties.getLogRetentionDays()); // 设置日志保留天数

        return xxlJobSpringExecutor; // 返回配置好的 XXL-JOB 执行器
    }
}
